package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadFile(MultipartFile file) {
        //校验file
        if (file!=null&&file.getSize()>0){
            //将文件上传到minIO  将路径返回
            //使用UUID生成文件名
            String fileName = UUID.randomUUID().toString().replace("-", "");
            String behindName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String filePath =null;
            try {
                filePath = fileStorageService.uploadImgFile("", fileName + behindName, file.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //将路径与用户ID保存在数据库   返回响应
            WmMaterial wmMaterial=new WmMaterial();
            wmMaterial.setUrl(filePath);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setType((short) 0);
            wmMaterial.setUserId(WmThreadLocalUtil.getWmUser().getId());
            save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 素材列表展示
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        //参数判断
        wmMaterialDto.checkParam();
        //分页查询
        IPage page=new Page(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        //查询条件
        LambdaQueryWrapper<WmMaterial> wrapper=new LambdaQueryWrapper<>();
        //1、是否收藏   如果前端传过来的是收藏  则执行该条件   不是收藏  不执行
        wrapper.eq(wmMaterialDto.getIsCollection()!=null&&wmMaterialDto.getIsCollection()==1,
                WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        //2、根据用户ID
        wrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getWmUser().getId());
        //3、根据创建时间降序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        //根据条件分页查询
        page(page,wrapper);
        //将结果封装并返回
        ResponseResult responseResult=new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 素材收藏
     * 根据素材id收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Long id) {
        //参数判断
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据id更新数据库
        LambdaUpdateWrapper<WmMaterial> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(WmMaterial::getId,id).set(WmMaterial::getIsCollection,1);
        update(wrapper);

        //返回响应

        return ResponseResult.okResult("收藏成功");
    }
}
